package com.itheima.sfbx.insurance.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sfbx.framework.commons.exception.ProjectException;
import com.itheima.sfbx.framework.commons.utils.BeanConv;
import com.itheima.sfbx.insurance.dto.CoefficentVO;
import com.itheima.sfbx.insurance.enums.CoefficentEnum;
import com.itheima.sfbx.insurance.mapper.CoefficentMapper;
import com.itheima.sfbx.insurance.pojo.CategoryCoefficent;
import com.itheima.sfbx.insurance.pojo.Coefficent;
import com.itheima.sfbx.insurance.pojo.InsuranceCoefficent;
import com.itheima.sfbx.insurance.service.ICategoryCoefficentService;
import com.itheima.sfbx.insurance.service.ICoefficentService;
import com.itheima.sfbx.insurance.service.IInsuranceCoefficentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Description：系数项服务实现类
 */
@Slf4j
@Service
public class CoefficentServiceImpl extends ServiceImpl<CoefficentMapper, Coefficent> implements ICoefficentService {


    /**
     * 系数项分页查询
     * @param coefficentVO 系数项查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return
     */
    @Override
    public Page<CoefficentVO> findPage(CoefficentVO coefficentVO, int pageNum, int pageSize) {
        Page<Coefficent> page = new Page<>(pageNum, pageSize);
        Page<Coefficent> page1 = this.lambdaQuery()
                .eq(StrUtil.isNotBlank(coefficentVO.getCoefficentKey()), Coefficent::getCoefficentKey,
                        coefficentVO.getCoefficentKey())
                .eq(StrUtil.isNotBlank(coefficentVO.getCoefficentKeyName()), Coefficent::getCoefficentKeyName,
                        coefficentVO.getCoefficentKeyName())
                .eq(StrUtil.isNotBlank(coefficentVO.getDataState()), Coefficent::getDataState,
                        coefficentVO.getDataState())
                .page(page);
        return BeanConv.toPage(page1, CoefficentVO.class);
    }

    @Override
    public CoefficentVO findById(String coefficentId) {
        //TODO
        return null;
    }

    /**
     * 新增系数项
     * @param coefficentVO 系数项查询条件
     * @return
     */
    @Override
    @Transactional
    public CoefficentVO save(CoefficentVO coefficentVO) {
        //判断系数项是否存在，系数项的key是不能重复的
        Integer count = this.lambdaQuery()
                .eq(StrUtil.isNotBlank(coefficentVO.getCoefficentKey()), Coefficent::getCoefficentKey,
                        coefficentVO.getCoefficentKey())
                .count();
        if (count > 0){
            throw new ProjectException(CoefficentEnum.SAVE_FAIL);
        }
        //判断之后直接保存
        Coefficent coefficent = BeanConv.toBean(coefficentVO, Coefficent.class);
        boolean result = save(coefficent);
        return BeanConv.toBean(coefficent, CoefficentVO.class);
    }


    @Autowired
    private IInsuranceCoefficentService coefficentService;

    @Autowired
    private ICategoryCoefficentService categoryCoefficentService;

    /**
     * 编辑系数项
     * @param coefficentVO 系数项对象
     * @return
     */
    @Override
    @Transactional
    public Boolean update(CoefficentVO coefficentVO) {
        //查询系数项是否存在
        Coefficent coefficent = getById(coefficentVO.getId());
        if (coefficent == null){
             throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
        //不能修改保险的分类和保险绑定的系数项
        Integer count = coefficentService.lambdaQuery()
                .eq(StrUtil.isNotBlank(coefficentVO.getCoefficentType()), InsuranceCoefficent::getCoefficentKey,
                        coefficentVO.getCoefficentKey())
                .count();
        Integer count1 = categoryCoefficentService.lambdaQuery()
                .eq(StrUtil.isNotBlank(coefficentVO.getCoefficentKey()), CategoryCoefficent::getCoefficentKey,coefficentVO.getCoefficentKey())
                .count();
        if (count > 0 || count1 > 0){
            throw new ProjectException(CoefficentEnum.UPDATE_FAIL);
        }
        //如果判断都没有问题，就修改
        Coefficent bean = BeanConv.toBean(coefficentVO, Coefficent.class);
        return updateById(bean);
    }

    /**
     * 删除系数项
     * @param checkedIds 选择中对象Ids
     * @return
     */
    @Override
    @Transactional
    public Boolean delete(String[] checkedIds) {
        List<String> ids = Arrays.asList(checkedIds);
        //根据ids将集合查出来
        List<Coefficent> coefficentList = this.lambdaQuery()
                .in(Coefficent::getId, ids)
                .list();
        if (CollUtil.isEmpty(coefficentList)){
            return true;
        }
        //根据ids将key查询来
        Set<String> keySet = coefficentList.stream().map(Coefficent::getCoefficentKey).collect(Collectors.toSet());
        //如果这些ids都存在，需要判断当前系数项是否有关联关系
        Integer count = coefficentService.lambdaQuery()
                .in(InsuranceCoefficent::getCoefficentKey,
                        keySet)
                .count();
        Integer count1 = categoryCoefficentService.lambdaQuery()
                .eq(CategoryCoefficent::getCoefficentKey,keySet)
                .count();
        if (count > 0 || count1 > 0){
            throw new ProjectException(CoefficentEnum.DEL_FAIL);
        }
        //如果没有关联关系就可以删除了
        return removeByIds(ids);
    }

    @Override
    public List<CoefficentVO> findList(CoefficentVO coefficentVO) {
        //TODO
        return null;
    }

    @Override
    public CoefficentVO findByCoefficentKey(String coefficentKey) {
        //TODO
        return null;
    }
}
